<template>
  <div class="sgin">
    <div class="s-l">
      <div class="title">合伙人中心</div>
    </div>

    <div class="s-r">
      <div class="login-title">登录</div>
      <div class="form">
        <div class="label">
          <span class="input-icon"></span>
          <span class="label-bar"></span>
          <input
            class="sgin-btn"
            v-model="username"
            type="text"
            placeholder="请输入用户名"
          />
        </div>

        <div class="label">
          <span class="input-icon input-pass-icon"></span>
          <span class="label-bar"></span>
          <span
            class="show-password"
            :class="{ 'show-password-active': showPassWord }"
            @click="showPassWord = !showPassWord"
          ></span>
          <input
            class="sgin-btn"
            v-model="password"
            @keyup.enter="handleSubmit"
            :type="showPassWord ? 'text' : 'password'"
            placeholder="请输入登录密码"
          />
        </div>
      </div>

      <div class="form-inline">
        <div class="rember" @click="remberUser = !remberUser">
          <span
            class="checked"
            :class="{ 'checked-active': remberUser }"
          ></span>
          <span>记住用户名</span>
        </div>
      </div>
      <el-button
        class="submit"
        type="primary"
        :loading="loading"
        @click="handleSubmit"
        @keyup.enter.native="handleSubmit"
        >登录</el-button
      >
    </div>
  </div>
</template>

<script>
import { loginBackground,userInfo } from '@/api/goods'
import { adminInfo } from '@/api/member'
import { setToken } from '@/utils/auth'

export default {
  data() {
    return {
      username: '',
      password: '',
      showPassWord: false,
      remberUser: false,
      loading: false
    }
  },

  mounted() {
    let un = localStorage.getItem('REMBER')
    if (un) {
      this.username = un
      this.remberUser = true
    }
  },

  methods: {
    async handleSubmit() {
      if (this.loading) return
      if (!this.username) {
        return this.$message.error('请输入用户名')
      }

      if (!this.password) {
        return this.$message.error('请输入密码')
      }

      // async getproductListData() {
      //       let res = await getProductList({});
      //       if (res.status !== 1) {
      //         return;
      //       }
      //     }
      //后面添加登录接口
      let res = await loginBackground({
        username: this.username,
        password: this.password
      })
      if (res.code === 200) {
        setToken(res.data.token)
        // let permissions = await getAllPermission()
        // console.log(permissions)
        console.log(res, 'username')
        // localStorage.setItem('USERNAME', res.data.admin.username)
        // localStorage.setItem('is_partner', res.data.admin.is_partner)
        if (this.remberUser) {
          localStorage.setItem('REMBER', this.username)
        }
        userInfo().then((res2)=>{
          localStorage.setItem("LOGIN_USERNAME", res2.data.username);
          sessionStorage.setItem("LOGIN_USERNAME_id", res2.data.id);
          sessionStorage.setItem("partnerId", res2.data.partnerId);
          sessionStorage.setItem("memberId", res2.data.memberId);
          
          adminInfo(res2.data.id).then((res3)=>{
            sessionStorage.setItem("roleId", res3.data.roleIds[0]);
          })
          // this.$router.push("/");
        }).catch((err2)=>{

        })
        this.$message({
          showClose: true,
          message: '登录成功',
          type: 'success',
          duration: 1000
        })
        setTimeout(() => {
          this.$router.push('/home/index')
        }, 2000);
        //跳转到首页
        
        //添加计时器 同时判断token
        // window.setInterval(function(){
        //   console.log('11111')
        // },1000)
      } else {
        this.$message.error(res.message)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.sgin {
  width: 1010px;
  height: 500px;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0px 17px 35px 0px rgba(30, 30, 30, 0.49);

  .s-l {
    float: left;
    height: 100%;
    width: 510px;
    background: rgba(255, 255, 255, 0.2);
    box-sizing: border-box;
    color: #fff;
    font-size: 16px;
    line-height: 30px;
    position: relative;

    .title {
      position: relative;
      top: 200px;
      width: 100%;
      padding: 0 50px;
      text-align: center;
      box-sizing: border-box;
      font-size: 25px;
      line-height: 38px;
    }
  }

  .s-r {
    float: right;
    width: 500px;
    height: 100%;
    box-sizing: border-box;
    position: relative;
    top: 0;
    left: 0;
    background: #fff;
    border-radius: 0;
    padding: 0 68px;

    .login-title {
      font-size: 22px;
      color: #333;
      line-height: 40px;
      text-align: center;
      margin: 80px 0 20px;
      border: none;
    }

    .submit {
      height: 46px;
      width: 100%;
      border-radius: 5px;
      font-size: 18px;
      margin-bottom: 15px;
    }
    .label {
      position: relative;
    }
    .label-bar {
      width: 1px;
      height: 34px;
      background: #d8d8d8;
      position: absolute;
      top: 6px;
      left: 40px;
    }

    .input-icon {
      width: 20px;
      height: 20px;
      position: absolute;
      top: 14px;
      left: 10px;
      background-image: url('../../assets/images/mvmc_login_icon.png');
      background-position: 0 0;
    }

    .sgin-btn {
      box-sizing: border-box;
      width: 100%;
      height: 46px;
      padding: 6px 10px 6px 54px;
      font-size: 16px;
      color: #333;
      line-height: 34px;
      border: none;
      border-radius: 5px;
      margin-bottom: 18px;
      background-color: #f3f5f8 !important;
      box-shadow: 0 0 1000000px #f3f5f8 inset !important;
      &::-webkit-autofill {
        background-color: #f3f5f8 !important;
        box-shadow: 0 0 1000000px #f3f5f8 inset !important;
        background-image: none;
      }
    }

    .input-pass-icon {
      background-position: -20px 0;
    }

    .show-password {
      width: 20px;
      height: 20px;
      position: absolute;

      background-image: url('../../assets/images/mvmc_login_icon.png');
      background-position: 0 -20px;
      top: 14px;
      right: 10px;
    }
    .show-password-active {
      background-position: -20px -20px;
    }

    .form-inline {
      margin-bottom: 12px;
      margin-top: 30px;
      font-size: 14px;
      line-height: 22px;
      color: #999;
      display: flex;
      justify-content: space-between;
      align-items: center;
      div {
        cursor: pointer;
        user-select: none;
      }

      .rember {
        color: #666;
        display: flex;
        align-items: center;
        .checked {
          width: 18px;
          height: 18px;
          box-sizing: border-box;
          border: 1px solid #e8e9eb;
          margin-right: 6px;
          display: flex;
          justify-content: center;
          align-items: center;
          border-radius: 2px;
        }

        .checked-active {
          &:after {
            content: '';
            width: 11px;
            height: 9px;
            background-image: url('../../assets/images/checkout_active.png');
            background-size: 100% 100%;
          }
        }
      }
    }
  }
}
</style>
